package com.liukai.user.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.user.R;

import java.util.List;

public class SettingRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_LOGOUT = 1;
    private static final int ITEM_OTHER = 2;

    // 我的信息界面功能列表
    private List<String> myInfoList;

    public SettingRecyclerAdapter(List<String> itemList){
        this.myInfoList = itemList;
    }

    @Override
    public int getItemViewType(int position) {
        String item = myInfoList.get(position);
        if (item.equals("退出登录")){
            return ITEM_LOGOUT;
        }else{
            return ITEM_OTHER;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_LOGOUT){
            view = View.inflate(parent.getContext(), R.layout.item_logout, null);
            return new MyInfoViewHolder(view);
        }else{
            view = View.inflate(parent.getContext(), R.layout.item_setting, null);
            return new MyInfoViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        MyInfoViewHolder myInfoViewHolder = (MyInfoViewHolder) viewHolder;

        final String itemName = myInfoList.get(position);

        myInfoViewHolder.mTvSetting.setText(itemName);
        myInfoViewHolder.mBtnItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClick.onItemClickListener(itemName);
            }
        });

    }

    @Override
    public int getItemCount() {
        return myInfoList.size();
    }

    public static class MyInfoViewHolder extends RecyclerView.ViewHolder{

        public TextView mTvSetting;
        public RelativeLayout mBtnItem;

        public MyInfoViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvSetting = itemView.findViewById(R.id.tv_setting);
            mBtnItem = itemView.findViewById(R.id.btn_item);

        }


    }

    public interface ItemClick{
        void onItemClickListener(String itemName);
    }


    // item点击事件的接口
    private ItemClick mItemClick;
    // 将点击事件接口传递过来
    public void setItemClick(ItemClick itemClick) {
        mItemClick = itemClick;
    }

}
